package com.ruoyi.controller;

import com.github.pagehelper.PageHelper;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.domain.Person;
import com.ruoyi.service.IPersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/person")
public class PersonController extends BaseController {

/**
 *  Person /  person
 */

    @Autowired
    private IPersonService personService;

    // 全查
    @GetMapping("/list")
    public AjaxResult list(){
        List<Person> list = personService.list();
        return AjaxResult.success(list);
    }

    // 条件查询/分页
    @GetMapping("/listForPage")
    public TableDataInfo listForPage(Person person){
        startPage();
        List<Person> list = personService.listForPage(person);

        return getDataTable(list);
    }

    // 根据id 查一条
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable Long id){
        System.out.println("id = " + id);
        Person person = personService.getById(id);
        return success(person);
    }

    // 添加保存
    @PostMapping()
    public AjaxResult add(@RequestBody Person person){
        System.out.println("person = " + person);
        int add = personService.save(person);
        return toAjax(add);
    }
    // 修改
    @PutMapping()
    public AjaxResult edit(@RequestBody Person person){
        System.out.println("person = " + person);
        int edit = personService.updateById(person);
        return toAjax(edit);
    }
//
//    @DeleteMapping("/{id}")
//    // 根据id 删除一条
//    public int deleteOne(@PathVariable Long id){
//        System.out.println("id = " + id);
//        int delete = personService.removeById(id);
//        return delete;
//    }
    @DeleteMapping("/{ids}")
    // 根据id 删除一条
    public AjaxResult deleteMany(@PathVariable Long[] ids){
        System.out.println("ids = " + Arrays.toString(ids));
        int delete = personService.removeByIds(ids);
        return toAjax(delete);
    }


    @PostMapping("/addMany")
    public AjaxResult addMany(){
        List<Person> persons = new ArrayList<>();
        Person p1 = new Person();
        p1.setCoId(397);
        p1.setPersName("p1");
        p1.setPersSex(0);
        p1.setPersInDate(new Date());
        p1.setPersSalary(0);
        p1.setPersRemark("1");
        persons.add(p1);

        Person p2 = new Person();
        p2.setCoId(397);
        p2.setPersName("p2");
        p2.setPersSex(1);
        p2.setPersInDate(new Date());
        p2.setPersSalary(10);
        p2.setPersRemark("2");
        persons.add(p2);

        int addCount = personService.saveMany(persons);
        System.out.println("addCount = " + addCount);


        return AjaxResult.success();
    }
}